const path = require('path')
const db = require(path.resolve('core/database/db.js'))
 
async function checkAccount(account){
  if(account === null){
    return Promise.reject('账号不能为空')
  }
  const finalAccount = account.trim();
  if(finalAccount.length != 11){
    return Promise.reject('账号必须为11位')
  }
  return finalAccount
}

async function checkPassword(password){
  if(password === null){
    return Promise.reject('密码不能为空')
  }
  const finalPWD = password.trim();
  if(finalPWD.length<1){
    return Promise.reject('密码不能为空')
  }
  if(finalPWD.length<6){
    return Promise.reject('密码最少6位')
  }
  if(finalPWD.length>12){
    return Promise.reject('密码不能超过12位')
  }
  return finalPWD
}

async function checkNickname(nickname){
  if(nickname === null){
    return Promise.reject('昵称不能为空')
  }
  const finalNickname = nickname.trim();
  if(finalNickname.length<1){
    return Promise.reject('昵称不能为空')
  }
  if(finalNickname.length<2){
    return Promise.reject('昵称最少2位')
  }
  if(finalNickname.length>8){
    return Promise.reject('昵称不能超过8位')
  }
  return finalNickname
}

async function queryUserExists(account) {
  try {
    const accountTemp = await checkAccount(account)
    return new Promise((resolve, reject)=>{
      db.query(
        `select * from user where phone=${accountTemp}`,{},
        (err, results)=> {
          if(err){
            reject('查询用户不存在')
            return
          }
          if(results === null || results.length<1){
            resolve(null)
          }
          resolve(results[0])
        })
    })
  } catch (error) {
    return Promise.reject(error)
  }
}

async function login(account, password){
  try {
    const accountTemp = await checkAccount(account)
    const user = await queryUserExists(accountTemp)
    if(user === null){
      return Promise.reject('用户不存在')
    }
    const pwdTemp = await checkPassword(password)
    return new Promise((resolve, reject)=>{
      db.query(
        `select * from user where phone=${accountTemp} and password=${pwdTemp}`,{},
        (err, results)=> {
          console.log(err);
          console.log(results);
          if(err || results === null || results.length<1){
            reject('账号密码不匹配')
            return
          }
          resolve(results)
        })
    })
  } catch (error) {
    return Promise.reject(error)
  }

}


async function register(account, password, nickname){
  try {
    const accountTemp = await checkAccount(account)
    const user = await queryUserExists(accountTemp);
    if(user != null){
      return Promise.reject('用户已存在')
    }
    const pwdTemp = await checkPassword(password)
    const nickTemp = await checkNickname(nickname)
    return new Promise((resolve, reject)=>{
      db.query(
        `INSERT INTO user SET ?`,
        {phone: accountTemp, nickname:nickTemp, password: pwdTemp},
        (err, results)=> {
          console.log(err);
          console.log(results);
          if(err){
            reject('注册失败')
            return
          }
          resolve('注册成功')
        })
    })
  } catch (error) {
    return Promise.reject(error)
  }
}

module.exports = {
  login,
  register,
}
